package cn.sunlelin.educationserver.service.Impl;

import cn.sunlelin.educationserver.entity.School;
import cn.sunlelin.educationserver.mapper.SchoolMapper;
import cn.sunlelin.educationserver.service.SchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SchoolServiceImpl implements SchoolService {
    @Autowired
    private SchoolMapper schoolMapper;
    @Override
    public Boolean createSchool(School school) {
        if(schoolMapper.selectSchoolByName(school.getSchoolName())!=null)
            return false;
        school.setCreateTime(new Date());
        schoolMapper.createSchool(school);
        return true;
    }

    @Override
    public Boolean updateSchool(School school) {
        School t=this.selectSchoolById(school.getSchoolId());
        if(t==null)
            return false;
        if(!t.getSchoolName().equals(school.getSchoolName())&&this.selectSchoolByName(school.getSchoolName())!=null)
            return false;
        school.setUpdateTime(new Date());
        schoolMapper.updateSchool(school);
        return true;
    }

    @Override
    public List<School> findAll(){
        return schoolMapper.findAll();
    }

    @Override
    public School selectSchoolById(Integer schoolId) {
        return schoolMapper.selectSchoolById(schoolId);
    }

    @Override
    public School selectSchoolByName(String schoolName) {
        return schoolMapper.selectSchoolByName(schoolName);
    }

    @Override
    public Boolean deleteSchool(Integer schoolId) {
        if(this.selectSchoolById(schoolId)==null)
            return false;
        School school=new School();
        school.setSchoolId(schoolId);
        school.setUpdateTime(new Date());
        schoolMapper.deleteSchool(school);
        return true;
    }
}
